package com.vito.dcs.sys.segregationSet.action;

import com.alibaba.dubbo.config.annotation.Reference;
import com.vito.base.authoriza.action.BaseAction;
import com.vito.base.authoriza.cache.SessionMap;
import com.vito.base.authoriza.model.UserModel;
import com.vito.base.model.ResMsg;
import com.vito.base.util.Validata;
import com.vito.dcs.sys.segregationSet.model.SegregationSetModel;
import com.vito.dcs.sys.segregationSet.service.SegreationSetService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import com.vito.base.model.Result;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 对应标段设置离析的参数
 */
@Controller
@RequestMapping("/dcs/sys/segreationSetAction")
public class SegreationSetAction extends BaseAction {

    private static final Logger log = LoggerFactory.getLogger(SegreationSetAction.class);

    @Reference
    private SegreationSetService segreationSetService;

    /**
     * 根据标段ID获取对应离析参数
     * @return
     */
    @RequestMapping("getBySecationId")
    @ResponseBody
    public Result getBySecationId(String sectionId){
        Result res = new Result();
        try{
            UserModel user = SessionMap.getObj();
            if(null == user){
                res.setCode(Result.CODE_LOGIN_REQUIRE);
                res.setMsg(ResMsg.MSG_LOGIN_NEED);
                return res;
            }
            res.setData(segreationSetService.getBySectionId(sectionId));
            res.setCode(Result.CODE_OK);
        }catch(Exception ex){
            log.error("查询失败",ex);
            res.setCode(Result.CODE_ERROR);
            res.setMsg(ResMsg.EXECERR);
        }
        return res;
    }

    /**
     * 保存离析参数值
     * @param segregationSetModel
     * @return
     */
    @RequestMapping("saveWaterGradation")
    @ResponseBody
    public Result saveSegregationSet(@RequestBody SegregationSetModel segregationSetModel){
        Result res = new Result();
        try{
            UserModel user = SessionMap.getObj();
            if(null == user){
                res.setCode(Result.CODE_LOGIN_REQUIRE);
                res.setMsg(ResMsg.MSG_LOGIN_NEED);
                return res;
            }
            segreationSetService.saveSegregationSet(segregationSetModel);
            res.setCode(Result.CODE_OK);
            res.setMsg(ResMsg.SAVESUC);
        }catch(Exception ex){
            log.error("保存失败",ex);
            res.setCode(Result.CODE_ERROR);
            res.setMsg(ResMsg.EXECERR);
        }
        return res;
    }

    /**
     * 根据标段ID更改离析参数值
     * @param segregationSetModel
     * @return
     */
    @RequestMapping("updateSegregationSet")
    @ResponseBody
    public Result updateSegregationSet(@RequestBody SegregationSetModel segregationSetModel){
        Result res = new Result();
        try{
            UserModel user = SessionMap.getObj();
            if(null == user){
                res.setCode(Result.CODE_LOGIN_REQUIRE);
                res.setMsg(ResMsg.MSG_LOGIN_NEED);
                return res;
            }
            Validata.isNullOrEmpty(segregationSetModel.getId(),"主键不能为空");
            segreationSetService.updateSegregationSet(segregationSetModel);
            res.setCode(Result.CODE_OK);
            res.setMsg(ResMsg.UPDSUC);
        }catch(Exception ex){
            log.error("更新失败",ex);
            res.setCode(Result.CODE_ERROR);
            res.setMsg(ResMsg.EXECERR);
        }
        return res;
    }
}
